package cn.lblbc.downloadlibdemo

import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import cn.lblbc.downloadlib.DownloadInfo

class DownloadTaskAdapter(
    private val tasks: MutableList<DownloadInfo>,
    private val onActionClickListener: (DownloadInfo, String) -> Unit
) : RecyclerView.Adapter<DownloadTaskAdapter.TaskViewHolder>() {

    companion object {
        private const val TAG = "DownloadTaskAdapter"
    }

    fun updateTasks(newTasks: List<DownloadInfo>) {
        Log.d(TAG, "updateTasks: Updating with ${newTasks.size} tasks")
        tasks.clear()
        tasks.addAll(newTasks)
        notifyDataSetChanged()
        Log.d(TAG, "updateTasks: Updated adapter, new size: ${tasks.size}")
    }

    fun addTask(task: DownloadInfo) {
        Log.d(TAG, "addTask: Adding task with ID: ${task.taskId}")
        tasks.add(task)
        notifyItemInserted(tasks.size - 1)
        Log.d(TAG, "addTask: Added task, new size: ${tasks.size}")
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TaskViewHolder {
        Log.d(TAG, "onCreateViewHolder: Creating new view holder")
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_download_task, parent, false)
        return TaskViewHolder(view)
    }

    override fun onBindViewHolder(holder: TaskViewHolder, position: Int) {
        Log.d(TAG, "onBindViewHolder: Binding view holder at position $position, task ID: ${tasks[position].taskId}")
        holder.bind(tasks[position])
    }

    override fun getItemCount(): Int {
        Log.d(TAG, "getItemCount: Returning count ${tasks.size}")
        return tasks.size
    }

    inner class TaskViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val taskIdTextView: TextView = itemView.findViewById(R.id.taskIdTextView)
        private val packageNameTextView: TextView = itemView.findViewById(R.id.packageNameTextView)
        private val statusTextView: TextView = itemView.findViewById(R.id.statusTextView)
        private val progressTextView: TextView = itemView.findViewById(R.id.progressTextView)
        private val fileInfoTextView: TextView = itemView.findViewById(R.id.fileInfoTextView)
        private val pauseButton: Button = itemView.findViewById(R.id.pauseButton)
        private val resumeButton: Button = itemView.findViewById(R.id.resumeButton)
        private val deleteButton: Button = itemView.findViewById(R.id.deleteButton)

        fun bind(task: DownloadInfo) {
            Log.d(TAG, "bind: Binding task with ID: ${task.taskId}")
            taskIdTextView.text = "任务ID: ${task.taskId}"
            packageNameTextView.text = "包名: ${task.packageName}"
            
            val statusText = when (task.status) {
                0 -> "未完成"
                1 -> "已完成"
                2 -> "失败"
                else -> "未知"
            }
            statusTextView.text = "状态: $statusText"
            
            val progress = if (task.totalSize > 0) {
                "%.1f%%".format(task.downloadedSize * 100.0 / task.totalSize)
            } else {
                "0%"
            }
            progressTextView.text = "进度: $progress (${task.downloadedSize}/${task.totalSize} bytes)"
            
            fileInfoTextView.text = "文件信息: 示例文件.apk (apk)"
            
            // 设置按钮点击事件
            pauseButton.setOnClickListener { 
                Log.d(TAG, "bind: Pause button clicked for task ${task.taskId}")
                onActionClickListener(task, "pause") 
            }
            resumeButton.setOnClickListener { 
                Log.d(TAG, "bind: Resume button clicked for task ${task.taskId}")
                onActionClickListener(task, "resume") 
            }
            deleteButton.setOnClickListener { 
                Log.d(TAG, "bind: Delete button clicked for task ${task.taskId}")
                onActionClickListener(task, "delete") 
            }
        }
    }
}